//   OData .NET Libraries
//   Copyright (c) Microsoft Corporation. All rights reserved.  
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.OData.Edm.Csdl.CsdlSemantics;
using Microsoft.OData.Edm.Csdl.Parsing;
using Microsoft.OData.Edm.Csdl.Parsing.Ast;
using Microsoft.OData.Edm.Validation;

namespace Microsoft.OData.Edm.Csdl
{
    /// <summary>
    /// Provides CSDL parsing services for EDM models.
    /// </summary>
    public static class CsdlReader
    {
        /// <summary>
        ///  Returns an IEdmModel for the given CSDL artifacts.
        /// </summary>
        /// <param name="readers">Collection of XmlReaders containing the CSDL artifacts.</param>
        /// <param name="model">The model generated by parsing.</param>
        /// <param name="errors">Errors reported while parsing.</param>
        /// <returns>Success of the parse operation.</returns>
        public static bool TryParse(IEnumerable<XmlReader> readers, out IEdmModel model, out IEnumerable<EdmError> errors)
        {
            return TryParse(readers, Enumerable.Empty<IEdmModel>(), out model, out errors);
        }

        /// <summary>
        ///  Returns an IEdmModel for the given CSDL artifacts.
        /// </summary>
        /// <param name="readers">Collection of XmlReaders containing the CSDL artifacts.</param>
        /// <param name="reference">Model to be references by the created model.</param>
        /// <param name="model">The model generated by parsing.</param>
        /// <param name="errors">Errors reported while parsing.</param>
        /// <returns>Success of the parse operation.</returns>
        public static bool TryParse(IEnumerable<XmlReader> readers, IEdmModel reference, out IEdmModel model, out IEnumerable<EdmError> errors)
        {
            return TryParse(readers, new IEdmModel[] { reference }, out model, out errors);
        }

        /// <summary>
        ///  Returns an IEdmModel for the given CSDL artifacts.
        /// </summary>
        /// <param name="readers">Collection of XmlReaders containing the CSDL artifacts.</param>
        /// <param name="references">Models to be references by the created model.</param>
        /// <param name="model">The model generated by parsing.</param>
        /// <param name="errors">Errors reported while parsing.</param>
        /// <returns>Success of the parse operation.</returns>
        public static bool TryParse(IEnumerable<XmlReader> readers, IEnumerable<IEdmModel> references, out IEdmModel model, out IEnumerable<EdmError> errors)
        {
            CsdlModel ast;
            if (CsdlParser.TryParse(readers, out ast, out errors))
            {
                model = new CsdlSemanticsModel(ast, new CsdlSemanticsDirectValueAnnotationsManager(), references);
                return true;
            }

            model = null;
            return false;
        }
    }
}
